package com.css.dzj.dzjportal.modules.supervision.service.impl;

import com.css.dzj.dzjportal.common.util.Constant;
import com.css.dzj.dzjportal.common.util.excel.ExcelData;
import com.css.dzj.dzjportal.common.util.excel.ExportExcelUtils;
import com.css.dzj.dzjportal.modules.supervision.entity.QSupervisionMatterEntity;
import com.css.dzj.dzjportal.modules.supervision.entity.SupervisionMatterEntity;
import com.css.dzj.dzjportal.modules.supervision.mapper.SupervisionMapper;
import com.css.dzj.dzjportal.modules.supervision.service.SupervisionService;
import com.querydsl.core.QueryResults;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * 监管事项清单service实现类
 */
@Service
public class SupervisionServiceImpl implements SupervisionService {
    @Autowired
    private HttpServletRequest request;

    @Autowired
    private SupervisionMapper supervisionMapper;

    @Autowired
    private JPAQueryFactory jpaQueryFactory;

    /**
     * 添加监管事项清单
     * @param supervisionMatterEntity
     */
    @Override
    @Transactional
    public void add(SupervisionMatterEntity supervisionMatterEntity) {
        supervisionMapper.save(supervisionMatterEntity);
    }


    /**
     * 通过id获取监管事项清单
     * @param id
     * @return
     */
    @Override
    public SupervisionMatterEntity queryById(String id) {
        return supervisionMapper.findById(id).get();
    }


    /**
     * 修改监管事项清单
     * @param supervisionMatterEntity
     */
    @Override
    @Transactional
    public void update(SupervisionMatterEntity supervisionMatterEntity) {

    }


    /**
     * 多条件分页查询监管事项清单
     * @param supervisionNumber
     * @param supervisionName
     * @param supervisionObject
     * @param permitType
     * @param supervisionForm
     * @param publicLocation
     * @param status
     * @param pageable
     * @return
     */
    @Override
    public QueryResults<SupervisionMatterEntity> listPage(String supervisionNumber, String supervisionName, String supervisionObject, String permitType, String supervisionForm, String publicLocation, String status, Pageable pageable) {
        QSupervisionMatterEntity qEntity = QSupervisionMatterEntity.supervisionMatterEntity;
        Predicate predicate = qEntity.isNotNull().or(qEntity.isNull());
        predicate = (supervisionNumber == null || supervisionNumber.isEmpty()) ? predicate : ExpressionUtils.and(predicate, qEntity.supervisionNumber.like("%" + supervisionNumber +"%"));
        predicate = (supervisionName == null || supervisionName.isEmpty()) ? predicate : ExpressionUtils.and(predicate, qEntity.supervisionName.like("%" + supervisionName + "%"));
        predicate = (supervisionObject == null || supervisionObject.isEmpty()) ? predicate : ExpressionUtils.and(predicate, qEntity.supervisionObject.like("%" + supervisionObject + "%"));
        predicate = (permitType == null || permitType.isEmpty()) ? predicate : ExpressionUtils.and(predicate, qEntity.permitType.eq(permitType));
        predicate = (supervisionForm == null || supervisionForm.isEmpty()) ? predicate : ExpressionUtils.and(predicate, qEntity.supervisionForm.eq(supervisionForm));
        predicate = (publicLocation == null || publicLocation.isEmpty()) ? predicate : ExpressionUtils.and(predicate, qEntity.publicLocation.eq(publicLocation));
        predicate = (status == null || status.isEmpty()) ? predicate : ExpressionUtils.and(predicate, qEntity.status.eq(status));
        predicate = ExpressionUtils.and(predicate, qEntity.delFlag.eq(Constant.NO_DELETE));
        return jpaQueryFactory.selectFrom(qEntity)
                .where(predicate)
                .orderBy(qEntity.createTime.desc())
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchResults();
    }


    /**
     * 分页查询所有监管事项清单
     * @param pageable
     * @return
     */
    @Override
    public QueryResults<SupervisionMatterEntity> getPageAll(Pageable pageable) {
        QSupervisionMatterEntity qEntity = QSupervisionMatterEntity.supervisionMatterEntity;
        Predicate predicate = qEntity.isNotNull().or(qEntity.isNull());
        predicate = ExpressionUtils.and(predicate, qEntity.delFlag.eq(Constant.NO_DELETE));
        return jpaQueryFactory.selectFrom(qEntity)
                .where(predicate)
                .orderBy(qEntity.createTime.desc())
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchResults();
    }

    /**
     * 分页查询所有已公开的监管事项清单
     * @param supervisionName
     * @param publicLocation
     * @param pageable
     * @return
     */
    @Override
    public QueryResults<SupervisionMatterEntity> queryPublic(String supervisionName, String publicLocation, Pageable pageable) {
        QSupervisionMatterEntity q = QSupervisionMatterEntity.supervisionMatterEntity;
        Predicate predicate = q.isNotNull().or(q.isNull());
        predicate = (supervisionName == null || supervisionName.isEmpty()) ? predicate : ExpressionUtils.and(predicate, q.supervisionName.like("%" + supervisionName + "%"));
     //   predicate = (publicLocation == null || publicLocation.isEmpty()) ? predicate : ExpressionUtils.and(predicate, q.publicLocation.like("%" + publicLocation + "%"));
        predicate = ExpressionUtils.and(predicate, q.status.eq("1"));
        return jpaQueryFactory.selectFrom(q)
                .where(predicate)
                .orderBy(q.createTime.desc())
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchResults();
    }

    /**
     *@Author weitao
     *@Date 13:49 2020/4/29
     * 根据参数查询，为导出提供
     */
    @Override
    public List<SupervisionMatterEntity> superList(String supervisionNumber, String supervisionName, String supervisionSubItem, String supervisionDept, String permitName, String permitType) {

        QSupervisionMatterEntity qEntity = QSupervisionMatterEntity.supervisionMatterEntity;
        Predicate predicate = qEntity.isNotNull().or(qEntity.isNull());
        predicate = (supervisionNumber == null || supervisionNumber.isEmpty()) ? predicate : ExpressionUtils.and(predicate, qEntity.supervisionNumber.like("%" + supervisionNumber +"%"));
        predicate = (supervisionName == null || supervisionName.isEmpty()) ? predicate : ExpressionUtils.and(predicate, qEntity.supervisionName.like("%" + supervisionName + "%"));
        predicate = (supervisionSubItem == null || supervisionSubItem.isEmpty()) ? predicate : ExpressionUtils.and(predicate, qEntity.supervisionSubItem.like("%" + supervisionSubItem + "%"));
        predicate = (supervisionDept == null || supervisionDept.isEmpty()) ? predicate : ExpressionUtils.and(predicate, qEntity.supervisionDept.like("%" + supervisionDept + "%"));
        predicate = (permitName == null || permitName.isEmpty()) ? predicate : ExpressionUtils.and(predicate, qEntity.permitName.like("%" + permitName +"%"));
        predicate = (permitType == null || permitType.isEmpty()) ? predicate : ExpressionUtils.and(predicate, qEntity.permitType.eq(permitType));
        predicate = ExpressionUtils.and(predicate, qEntity.delFlag.eq(Constant.NO_DELETE));

       List<SupervisionMatterEntity> superList = jpaQueryFactory.selectFrom(qEntity)
                .where(predicate)
                .orderBy(qEntity.createTime.desc())
                .fetch();

       return  superList;
    }

    /**
     * 根据参数导出监管事项清单
     */
    @Override
    public void exportSuperToExcel(HttpServletResponse response, List<SupervisionMatterEntity> superList) {

        if (superList == null || superList.size() < 0) {
            throw new RuntimeException("监管事项清单为空");
        }
        // 创建导出实体
        ExcelData data = new ExcelData();
        data.setName("监管事项清单");
        /**
         * 设置Excel标题
         */
        ArrayList<String> titles = new ArrayList<>();
        titles.add("监管事项编号");
        titles.add("监管事项名称");
        titles.add("监管事项子项");
        titles.add("监管部门");
        titles.add("对应的许可事项名称");
        titles.add("对应的许可事项类型");
        data.setTitles(titles);
        /**
         * 处理行数据
         */
        ArrayList<List<Object>> rows = new ArrayList<>();
        for (SupervisionMatterEntity superEntity : superList) {
            ArrayList<Object> row = new ArrayList<>();
            row.add(superEntity.getSupervisionNumber());
            row.add(superEntity.getSupervisionName());
            row.add(superEntity.getSupervisionSubItem());
            row.add(superEntity.getSupervisionDept());
            row.add(superEntity.getPermitName());
            row.add(superEntity.getPermitType());
            rows.add(row);
        }
        data.setRows(rows);
        try {
            // 导出
            ExportExcelUtils.exportExcel(request,response, "监管事项清单.xlsx", data);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 导出选中行数据 通过ids
     */
    @Override
    public List<SupervisionMatterEntity> queryByIds(String ids) {

        if (com.alibaba.druid.util.StringUtils.isEmpty(ids)) {
            throw new RuntimeException("ids为空!");
        }
        List<String> idList = Arrays.asList(ids.split(","));
        List<SupervisionMatterEntity> superList = supervisionMapper.findAllById(idList);

        return superList;
    }

    /**
     * 多条件查询监管事项清单
     */
    @Override
    public QueryResults<SupervisionMatterEntity> wtlistPage(String supervisionNumber, String supervisionName, String supervisionSubItem, String supervisionDept, String permitName, String permitType, Pageable pageable) {

        QSupervisionMatterEntity qEntity = QSupervisionMatterEntity.supervisionMatterEntity;
        Predicate predicate = qEntity.isNotNull().or(qEntity.isNull());
        predicate = (supervisionNumber == null || supervisionNumber.isEmpty()) ? predicate : ExpressionUtils.and(predicate, qEntity.supervisionNumber.like("%" + supervisionNumber +"%"));
        predicate = (supervisionName == null || supervisionName.isEmpty()) ? predicate : ExpressionUtils.and(predicate, qEntity.supervisionName.like("%" + supervisionName + "%"));
        predicate = (supervisionSubItem == null || supervisionSubItem.isEmpty()) ? predicate : ExpressionUtils.and(predicate, qEntity.supervisionSubItem.like("%" + supervisionSubItem + "%"));
        predicate = (supervisionDept == null || supervisionDept.isEmpty()) ? predicate : ExpressionUtils.and(predicate, qEntity.supervisionDept.like("%" + supervisionDept + "%"));
        predicate = (permitName == null || permitName.isEmpty()) ? predicate : ExpressionUtils.and(predicate, qEntity.permitName.like("%" + permitName +"%"));
        predicate = (permitType == null || permitType.isEmpty()) ? predicate : ExpressionUtils.and(predicate, qEntity.permitType.eq(permitType));
        predicate = ExpressionUtils.and(predicate, qEntity.delFlag.eq(Constant.NO_DELETE));

        return jpaQueryFactory.selectFrom(qEntity)
                .where(predicate)
                .orderBy(qEntity.createTime.desc())
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchResults();
    }

    /**
     * 统计数
     */
    @Override
    public String getSupervCount() {
        QSupervisionMatterEntity qEntity = QSupervisionMatterEntity.supervisionMatterEntity;
        Long l = jpaQueryFactory.select(qEntity.id.count())
                                .from(qEntity)
                                .where(qEntity.delFlag.eq(Constant.NO_DELETE))
                                .fetchCount();
        if(l!=null){
            return  l.toString();
        }
        return "0";
    }


}
